package org.platform.lwc.redis.factory.lettuce;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

import java.util.stream.Collectors;

/**
 * @Description: 哨兵redis
 * @Author: lwc
 * @Date: 2021/5/20
 */
public class SentinelObtainLettuceConnection implements ObtainLettuceConnection {

    @Override
    public LettuceConnectionFactory obtainLettuceConnection(RedisProperties redisProperties) {
        RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(sentinel.getMaster(), sentinel.getNodes().stream().collect(Collectors.toSet()));
        redisSentinelConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        redisSentinelConfiguration.setDatabase(redisProperties.getDatabase());
        LettucePoolingClientConfiguration lettuceClientConfiguration = getLettucePoolingClientConfiguration(redisProperties);
        return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration);
    }
}
